﻿Imports Catalyst.Web.DynamicData

Partial Public Class ForeignKey_MultiSelect
    Inherits Catalyst.Web.DynamicData.InFilterUserControlBase

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        PopulateListControl(ListBox1)
    End Sub

    Public Overrides Property ValueList() As String
        Get
            Dim sb As New StringBuilder()
            For Each li As ListItem In ListBox1.Items
                If li.Selected Then
                    sb.AppendFormat("{0}", li.Value)
                End If
            Next
            Return sb.ToString()
        End Get
        Set(ByVal value As String)
            ListBox1.SelectedIndex = -1
            Dim values As String() = value.Split(","c)
            For Each val As String In values
                Dim li As ListItem = ListBox1.Items.FindByValue(val)
                If li IsNot Nothing Then
                    li.Selected = True
                End If
            Next
        End Set
    End Property

    Public Overrides ReadOnly Property ValueType() As System.TypeCode
        Get
            Return ForeignKeyColumn.ParentTable.PrimaryKeyColumns(0).TypeCode
        End Get
    End Property

    Public Overrides Function GetWhereParameters(ByVal dataSource As System.Web.DynamicData.IDynamicDataSource) As System.Collections.Generic.IEnumerable(Of System.Web.UI.WebControls.Parameter)

        Dim param As New InExpressionParameter()
        With param
            .Name = String.Format("{0}.{1}", Column.Name, ForeignKeyColumn.ParentTable.PrimaryKeyColumns(0).Name)
            .ValueType = ValueType
            .ValueList = ValueList
        End With

        Return New Parameter() {param}

    End Function

End Class